<?php
  include("./include/config.inc.php");
  if(!isset($_SESSION['s_activId']))
  {
    header('location:checkLogin.php');
  }
  $today = date('Y-m-d');
  $displayArray = array();
  $m= 0;
  
  //Received Installment Process
  if(isset($_REQUEST['installmentReceived']))
  {
    foreach ($_REQUEST['installmentReceived'] as $key => $value)
	  {
	    if($value > 0)
		  {
		    echo "<BR> ".$key." : ".$value;
		    for($i=1;$i<=$value;$i++)
			  {
			    
			  }
		  }
    }
      echo $updateReceived = "UPDATE interestreceived
                            SET receivedDate = '".$today."',
                                isReceive = '1'
                          WHERE interestReceivedId = ";
      $updateReceivedRes = mysql_query($updateReceived);
  }
  
  //select record for display on submit 
  if(isset($_POST['submitBtn']))
  {
  	$todate = $_POST['toDateYear']."-".$_POST['toDateMonth']."-".$_POST['toDateDay'];
    if(isset($_POST['loanNo']) && $_POST['loanNo']!= "")
    {
    	//Logic of insert a recoard in installment table..........Start
      $chkInterestToday = "SELECT loanId, pendingAmount, monthlyInterestRs, loanDate
                             FROM loan 
                            WHERE loan.loanDate < '".$todate."'
                              AND loanId = ".$_POST['loanNo'];
      $chkInterestTodayRes = mysql_query($chkInterestToday);
      $chkInterestTodayNumRows = mysql_num_rows($chkInterestTodayRes);
      if($chkInterestTodayNumRows > 0)
      {
	      while($chkInterestTodayRow = mysql_fetch_array($chkInterestTodayRes))
	      {
	        for($i = $chkInterestTodayRow['loanDate'];$i <= $todate;$i=date('Y-m-d',strtotime('+30 day',strtotime($i))))
	        {
            $chkLoneavailableRecord = "SELECT `loanId` 
                                         FROM `interestreceived`
                                        WHERE `dateOfInstallment` = '".$i."'
                                          AND `loanId` = ".$chkInterestTodayRow['loanId'];
            $chkLoneavailableRecordRes = mysql_query($chkLoneavailableRecord);
            $chkLoneavailableRecordNumRows = mysql_num_rows($chkLoneavailableRecordRes);
            if($chkLoneavailableRecordNumRows == 0)
            {
              $insertInterestRecived = "INSERT INTO `interestreceived` (`loanId`, `pendingAmount`, `interest`, `dateOfInstallment`, `receivedDate`, `isReceive`)
                                             VALUES ('".$chkInterestTodayRow['loanId']."','".$chkInterestTodayRow['pendingAmount']."',
                                                    '".$chkInterestTodayRow['monthlyInterestRs']."','".$i."','0000-00-00',0)";
              $insertInterestRecivedRes = mysql_query($insertInterestRecived);
            }
          }
        }
      }
      //Logic of insert a recoard in installment table..........End
      
      //Query for display loan installment.........Start 
      $selectForDisplay = "SELECT interestreceived.interestReceivedId, interestreceived.loanId, party.partyName, interestreceived.interest, interestreceived.dateOfInstallment 
                             FROM interestreceived
                             JOIN loan ON loan.loanId = interestreceived.loanID 
                             JOIN party ON loan.partyId = party.partyID 
                            WHERE isReceive = 0
                              AND interestreceived.loanId=".$_POST['loanNo'];
      $selectForDisplayRes = mysql_query($selectForDisplay);
      while($selectForDisplayRow = mysql_fetch_array($selectForDisplayRes))
      {
        $displayArray[$m]['interestReceivedId'] = $selectForDisplayRow['interestReceivedId'] ;
        $displayArray[$m]['loanId'] = $selectForDisplayRow['loanId'] ;
        $displayArray[$m]['partyName'] = $selectForDisplayRow['partyName'] ;
        $displayArray[$m]['interest'] = $selectForDisplayRow['interest'] ;
        $displayArray[$m]['dateOfInstallment'] = $selectForDisplayRow['dateOfInstallment'] ;
        $m++;
      }
      //Query for display loan installment.........End
    }
    else if(isset($_POST['partyNameSel']) != 0)
    {
    	$_POST['partyNameSel'];
    	$selectByParty = "SELECT loanId
    											FROM loan
    										 WHERE partyId = ".$_POST['partyNameSel'];
      $selectByPartyRes = mysql_query($selectByParty);
      while($selectByPartyRow = mysql_fetch_array($selectByPartyRes))
      {
        $chkInterestTodayByParty = "SELECT loanId, pendingAmount, monthlyInterestRs, loanDate
                                      FROM loan 
                                     WHERE loan.loanDate < '".$todate."'
                                       AND loanId = ".$selectByPartyRow['loanId'];
        $chkInterestTodayByPartyRes = mysql_query($chkInterestTodayByParty);
        $chkInterestTodayByPartyRow = mysql_fetch_array($chkInterestTodayByPartyRes);
        $chkInterestTodayByPartyNumRows = mysql_num_rows($chkInterestTodayByPartyRes);
        if($chkInterestTodayByPartyNumRows > 0)
        {
          for($i = $chkInterestTodayByPartyRow['loanDate'];$i <= $todate;$i=date('Y-m-d',strtotime('+30 day',strtotime($i))))
	        {
            $chkLoneavailableRecord = "SELECT `loanId` 
                                         FROM `interestreceived`
                                        WHERE `dateOfInstallment` = '".$i."'
                                          AND `loanId` = ".$chkInterestTodayByPartyRow['loanId'];
            $chkLoneavailableRecordRes = mysql_query($chkLoneavailableRecord);
            $chkLoneavailableRecordNumRows = mysql_num_rows($chkLoneavailableRecordRes);
            if($chkLoneavailableRecordNumRows == 0)
            {
              $insertInterestRecived ="INSERT INTO `interestreceived` (`loanId`, `pendingAmount`, `interest`, `dateOfInstallment`, `receivedDate`, `isReceive`)
                                             VALUES ('".$chkInterestTodayByPartyRow['loanId']."','".$chkInterestTodayByPartyRow['pendingAmount']."',
                                                    '".$chkInterestTodayByPartyRow['monthlyInterestRs']."','".$i."','0000-00-00',0)";
              $insertInterestRecivedRes = mysql_query($insertInterestRecived);
            }
          }
        }
      }
      //Query for display loan installment.........Start 
      $selectForDisplay = "SELECT interestreceived.interestReceivedId, interestreceived.loanId, party.partyName, interestreceived.interest, interestreceived.dateOfInstallment 
                             FROM interestreceived
                             JOIN loan ON loan.loanId = interestreceived.loanID 
                             JOIN party ON loan.partyId = party.partyID 
                            WHERE isReceive = 0
                              AND party.partyId=".$_POST['partyNameSel'];
      $selectForDisplayRes = mysql_query($selectForDisplay);
      while($selectForDisplayRow = mysql_fetch_array($selectForDisplayRes))
      {
        $displayArray[$m]['interestReceivedId'] = $selectForDisplayRow['interestReceivedId'] ;
        $displayArray[$m]['loanId']             = $selectForDisplayRow['loanId'] ;
        $displayArray[$m]['partyName']          = $selectForDisplayRow['partyName'] ;
        $displayArray[$m]['interest']           = $selectForDisplayRow['interest'] ;
        $displayArray[$m]['dateOfInstallment']  = $selectForDisplayRow['dateOfInstallment'] ;
        $m++;
      }
      //Query for display loan installment.........End
    }
    if($_POST['partyNameSel'] == 0 && $_POST['loanNo'] == "")
    {
    	$selectAll = "SELECT loanId
    											FROM loan";
      $selectAllRes = mysql_query($selectAll);
      while($selectAllRow = mysql_fetch_array($selectAllRes))
      {
        $chkInterestTodayAll = "SELECT loanId, pendingAmount, monthlyInterestRs, loanDate
                                  FROM loan 
                                 WHERE loan.loanDate < '".$todate."'
                                   AND loanId = ".$selectAllRow['loanId'];
        $chkInterestTodayAllRes = mysql_query($chkInterestTodayAll);
        $chkInterestTodayAllRow = mysql_fetch_array($chkInterestTodayAllRes);
        $chkInterestTodayAllRows = mysql_num_rows($chkInterestTodayAllRes);
        if($chkInterestTodayAllRows > 0)
        {
          for($i = $chkInterestTodayAllRow['loanDate'];$i <= $todate;$i=date('Y-m-d',strtotime('+30 day',strtotime($i))))
	        {
            $chkLoneavailableRecord = "SELECT `loanId` 
                                         FROM `interestreceived`
                                        WHERE `dateOfInstallment` = '".$i."'
                                          AND `loanId` = ".$selectAllRow['loanId'];
            $chkLoneavailableRecordRes = mysql_query($chkLoneavailableRecord);
            $chkLoneavailableRecordNumRows = mysql_num_rows($chkLoneavailableRecordRes);
            if($chkLoneavailableRecordNumRows == 0)
            {
              $insertInterestRecived = "INSERT INTO `interestreceived` (`loanId`, `pendingAmount`, `interest`, `dateOfInstallment`, `receivedDate`, `isReceive`)
                                             VALUES ('".$chkInterestTodayAllRow['loanId']."','".$chkInterestTodayAllRow['pendingAmount']."',
                                                    '".$chkInterestTodayAllRow['monthlyInterestRs']."','".$i."','0000-00-00',0)";
              $insertInterestRecivedRes = mysql_query($insertInterestRecived);
            }
          }
        }
      }
      //Query for display loan installment.........Start 
      $selectForDisplay = "SELECT interestreceived.interestReceivedId, interestreceived.loanId, party.partyName, interestreceived.interest, interestreceived.dateOfInstallment 
                             FROM interestreceived
                             JOIN loan ON loan.loanId = interestreceived.loanID 
                             JOIN party ON loan.partyId = party.partyID 
                            WHERE isReceive = 0";
      $selectForDisplayRes = mysql_query($selectForDisplay);
      while($selectForDisplayRow = mysql_fetch_array($selectForDisplayRes))
      {
        $displayArray[$m]['interestReceivedId'] = $selectForDisplayRow['interestReceivedId'] ;
        $displayArray[$m]['loanId']             = $selectForDisplayRow['loanId'] ;
        $displayArray[$m]['partyName']          = $selectForDisplayRow['partyName'] ;
        $displayArray[$m]['interest']           = $selectForDisplayRow['interest'] ;
        $displayArray[$m]['dateOfInstallment']  = $selectForDisplayRow['dateOfInstallment'] ;
        $m++;
      }
      //Query for display loan installment.........End
    }
  }
  
  
  //select party for party combo
  $arrParty = array();
  $i=0;
  $selParty="SELECT `partyId`,`partyName` 
               FROM `party`";
  $selPartyRes=mysql_query($selParty);
  while($selPartyResRow=mysql_fetch_array($selPartyRes))
  {
    $arrParty['partyName'][$i] = $selPartyResRow['partyName'];
    $arrParty['partyId'][$i]   = $selPartyResRow['partyId'];
    $i++;
  }
  
  include("./bottom.php");
  $smarty->assign('today',$today);  
  $smarty->assign('displayArray',$displayArray);  
  $smarty->assign('arrParty',$arrParty);  
  $smarty->display('loanDetail.tpl');
?>